/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */
package com.nai365.nart.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nai365.nart.operation.dto.SpuDto;
import com.nai365.nart.operation.dto.PledgeDemandDto;
import com.nai365.nart.operation.entity.PledgeDemand;
import com.nai365.nart.operation.mapper.PledgeDemandMapper;
import com.nai365.nart.operation.service.PledgeDemandService;
import com.nai365.nart.operation.service.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 质押需求
 *
 * @author baogs
 * @date 2022-07-01 11:13:17
 */
@Service
@AllArgsConstructor
public class PledgeDemandServiceImpl extends ServiceImpl<PledgeDemandMapper, PledgeDemand> implements PledgeDemandService {


	private final PledgeDemandMapper pledgeDemandMapper;

	private final ProductService productService;


	@Override
	public Page<PledgeDemandDto> page(Page<PledgeDemand> page, QueryWrapper<PledgeDemand> queryWrapper) {
		List<PledgeDemandDto> result = new ArrayList<>();
		//查询分页数据
		page = pledgeDemandMapper.selectPage(page, queryWrapper);
		Page<PledgeDemandDto> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
		//组装使用到的会员ID列表，SPU ID列表
		List<Long> spuIdList = new ArrayList<>();
		for (PledgeDemand p : page.getRecords()) {
			if (p.getSpuId() != null) {
				spuIdList.add(p.getSpuId());
			}
		}
		//远程查询类别、SPU、会员数据
		Map<Long, SpuDto> artifactDtoMap = this.productService.getSpuInfoMapByIds(spuIdList);
		//组装结果数据
		for (PledgeDemand p : page.getRecords()) {
			PledgeDemandDto vo = pledgeDemand2PledgeDemandDto(p, artifactDtoMap);
			result.add(vo);
		}
		resultPage.setRecords(result);
		return resultPage;
	}

	public PledgeDemandDto pledgeDemand2PledgeDemandDto(PledgeDemand pledgeDemand, Map<Long, SpuDto> artifactDtoMap) {
		PledgeDemandDto dto = new PledgeDemandDto();
		BeanUtils.copyProperties(pledgeDemand, dto);
		SpuDto artifactDto = artifactDtoMap.get(pledgeDemand.getSpuId());
		if (artifactDto != null) {
			dto.setSpu(artifactDto);
		} else {
			artifactDto = new SpuDto();
			artifactDto.setId(pledgeDemand.getSpuId());
		}
		return dto;
	}

	@Override
	public PledgeDemandDto getPledgeDemandDtoById(Long id) {
		PledgeDemand pledgeDemand = this.getById(id);
		List<Long> spuIdList = new ArrayList<>();
		spuIdList.add(pledgeDemand.getSpuId());
		Map<Long, SpuDto> artifactDtoMap = this.productService.getSpuInfoMapByIds(spuIdList);
		return pledgeDemand2PledgeDemandDto(pledgeDemand, artifactDtoMap);
	}
}
